package com.zz.controller;

import com.zz.entity.User;
import com.zz.pojo.ResponseBo;
import com.zz.service.UserService;
import com.zz.util.KeyUtil;
import com.zz.util.MD5Utils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;


@Controller
@RequestMapping("user")
public class UserController {
	@Resource
    UserService userService;

	@GetMapping("/register")
	public String login() {
		return "register.html";
	}

	@PostMapping("/register")
	@ResponseBody
	public ResponseBo register(User user) {
		
		String password = MD5Utils.encrypt(user.getName(), user.getPwd());
		user.setPwd(password);
		user.setId(KeyUtil.genUniqueKey());
		userService.save(user);
		return ResponseBo.ok();
	}

	@RequestMapping("/")
	public String redirectIndex() {
		return "redirect:/index";
	}

	@RequestMapping("/index")
	public String index(Model model) {
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		model.addAttribute("user", user);
		return "index1.html";
	}
	
	@PostMapping("/getlogin")
	@ResponseBody
	public User getLoginUser(){
		return (User) SecurityUtils.getSubject().getPrincipal();
	}
}